package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HatchetIssBank;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.domain.HatchetPlan;
import com.ruoyi.system.service.IHatchetIssBankService;
import com.ruoyi.system.service.IHatchetMerchantIncreaseLimitService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.base.BaseController;

/**
 * 信用卡 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-23
 */
@Controller	
@RequestMapping("/system/hatchetMerchantIncreaseLimitForJLR")
public class HatchetMerchantIncreaseLimitForJLRController extends BaseController
{
    private String prefix = "system/hatchetMerchantIncreaseLimit";
	
	@Autowired
	private IHatchetMerchantIncreaseLimitService hatchetMerchantIncreaseLimitService;
	@Autowired
	private IHatchetMerchantService  merchantService;
	@Autowired
	private IHatchetIssBankService  bankService;
	@RequiresPermissions("system:hatchetMerchantIncreaseLimit:view")
	@GetMapping()
	public String hatchetMerchantIncreaseLimitForJLR()
	{
	    return prefix + "/hatchetMerchantIncreaseLimitForJLR";
	}
	
	//跳转查询账单日为当天往后的五天内的卡 比如 今天1号 查询账单日为2号到6的卡
	@GetMapping("/ByZDR")
	public String hatchetMerchantIncreaseLimitForJLRByZDR()
	{
	    return prefix + "/hatchetMerchantIncreaseLimitForJLRByZDR";
	}
	/**
	 * 查询信用卡列表
	 */
	@RequiresPermissions("system:hatchetMerchantIncreaseLimit:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
	{
		HatchetMerchant merchant =  merchantService.selectHatchetMerchantByMerchantNo(getLoginName());
		//设置经理人id
		if (StringUtils.isNotNull(merchant)) {
			hatchetMerchantIncreaseLimit.setAuditRecord(merchant.getID());
		} else {
			throw new RuntimeException("当前登陆用户异常");
		}
		HatchetMerchant paramMerchant = new HatchetMerchant();
		if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit.getMerchantId())||
				StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit.getMerchantPhone())) {
			//通过用户编号查询用户
			paramMerchant.setMerchantNo(hatchetMerchantIncreaseLimit.getMerchantId());
			paramMerchant.setPHONE(hatchetMerchantIncreaseLimit.getMerchantPhone());
			List<HatchetMerchant> merchantList = merchantService.selectHatchetMerchantList(paramMerchant);
			if (StringUtils.isNotEmpty(merchantList) && merchantList.size()>0) {
				hatchetMerchantIncreaseLimit.setMerchantId(merchantList.get(0).getID());
			}else {
				return getDataTable(new ArrayList<>());
			}
		}
		//正常使用的卡
		hatchetMerchantIncreaseLimit.setIncreaseLimitStatus("10B");
		startPage();
        List<HatchetMerchantIncreaseLimit> list = hatchetMerchantIncreaseLimitService.selectHatchetMerchantIncreaseLimitListForJLR(hatchetMerchantIncreaseLimit);
		for (HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit2 : list) {
			if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit2.getBankName())) {
				//查询银行名称
				HatchetIssBank bank = bankService.selectHatchetIssBankByBankCode(hatchetMerchantIncreaseLimit2.getBankName());
				hatchetMerchantIncreaseLimit2.setBankName(bank.getBankName());
			}
			//身份证号银行卡号加密
			if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit2.getIdCardNumber())) {
				hatchetMerchantIncreaseLimit2.setIdCardNumber(StringUtils.idCardNoString(hatchetMerchantIncreaseLimit2.getIdCardNumber(),4));
			}
			//银行卡号加密
			if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit2.getBankAccount())) {
				hatchetMerchantIncreaseLimit2.setBankAccount(StringUtils.idCardNoString(hatchetMerchantIncreaseLimit2.getBankAccount(),4));
			}
			//银行预留手机号加密
			if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit2.getBankPhone())) {
				hatchetMerchantIncreaseLimit2.setBankPhone(StringUtils.phoneString(hatchetMerchantIncreaseLimit2.getBankPhone()));
			}
			//匹配用户信息
			HatchetMerchant merchant2 = merchantService.selectHatchetMerchantById(hatchetMerchantIncreaseLimit2.getMerchantId());
			if (StringUtils.isNotNull(merchant2)) {
				hatchetMerchantIncreaseLimit2.setMerchantId(merchant2.getMerchantNo());
				hatchetMerchantIncreaseLimit2.setMerchantName(merchant2.getMerchantCnName());
				//借用examineResult 显示用户手机号码
				hatchetMerchantIncreaseLimit2.setMerchantPhone(merchant2.getPHONE());
			}
			//查看信用卡是否有计划在跑
			List<HatchetPlan> hatchetPlans = hatchetMerchantIncreaseLimitService.selectPlanByLimit(hatchetMerchantIncreaseLimit2);
			if (StringUtils.isNotEmpty(hatchetPlans) && hatchetPlans.size()>0) {
				//借用字段 隐藏制定计划按钮
				hatchetMerchantIncreaseLimit2.setCvn("N");
			}
			//查看信用卡委托状态 从auditRecord中读取 例如 10A:572069E3D55C4473A94E7317B4C52839
			if (StringUtils.isNotEmpty(hatchetMerchantIncreaseLimit2.getAuditRecord())) {
				String auditRecord = hatchetMerchantIncreaseLimit2.getAuditRecord();
				String[] split = auditRecord.split(":");
				if (split.length>1) {
					hatchetMerchantIncreaseLimit2.setEntrustStatus(split[0]);
				}
			}
		}
        return getDataTable(list);
	}
	/**
	 *处理委托
	 */
	@RequiresPermissions("system:hatchetMerchantIncreaseLimit:edit")
    @PostMapping("/doEntrust")
    @ResponseBody
    public Map<String, Object> doEntrust(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
    {
		Map<String, Object> map = new HashMap<>();
		try {
			map = hatchetMerchantIncreaseLimitService.doEntrust(hatchetMerchantIncreaseLimit);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", BusinessStatus.FAIL);
			map.put("msg", e.getMessage());
		}
		return map;
    }
	
	/**
	 * 修改保存信用卡的备注
	 */
	@RequiresPermissions("system:hatchetMerchantIncreaseLimit:edit")
	@Log(title = "信用卡", businessType = BusinessType.UPDATE)
	@PostMapping("/saveRemark")
	@ResponseBody
	public Map<String,Object> editSave(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
	{		
		Map<String,Object> map = new HashMap<>();
		int update = hatchetMerchantIncreaseLimitService.updateHatchetMerchantIncreaseLimit(hatchetMerchantIncreaseLimit);
		if (update>0) {
			map.put("msg", "保存成功");
			map.put("status", BusinessStatus.SUCCESS);
		} else {
			map.put("msg", "保存失败请稍后再试");
			map.put("status", BusinessStatus.FAIL);
		}
		return map;
	}
	
}
